<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="css/style.css">
    <style>
        .container {
            width: 100%;
            height: 100vh;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
        }

        .container button {
            padding: 10px 20px;
            margin: 10px;
            font-size: 16px;
            cursor: pointer;
            display: inline-block;
        }

        .message {
            width: 100%;
            height: 80px;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            font-size: 20px;
            color: red;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="message">
            <p id="message"></p>
        </div>
        <div>
            <button id="btn">Fetch</button>
            <button id="clearbtn">Clear</button>
        </div>
    </div>
    <script>

        const btn = document.querySelector('#btn');
        const message = document.querySelector('#message');
        const clearbtn = document.querySelector('#clearbtn');
        btn.addEventListener('click', async () => {
            try {
                const response = await fetch('http://localhost:3001/');
                const contentType = response.headers.get('Content-Type');

                if (contentType && contentType.includes('application/json')) {
                    const data = await response.json();
                    message.innerHTML = data.message;
                } else if (contentType && (contentType.includes('text/plain')) || contentType.includes('text/html')) {
                    const data = await response.text();
                    message.innerHTML = data;
                } else {
                    // Handle other content types or unknown types
                    message.innerHTML = 'Unknown content type';
                }
            } catch (e) {
                message.innerHTML = e.message;
            }
        });
        clearbtn.onclick = () => {
            message.innerHTML = '';
        }

    </script>

</body>

</html>